<template>
  <el-dialog
    :title="`${title}角色`"
    :visible.sync="isShow"
    width="30%"
    @close="closeForm"
  >
    <!-- 表单 -->
    <el-form ref="form" :model="roleForm" :rules="rules" label-width="120px">
      <el-form-item label="角色名称:" prop="role_name">
        <el-input v-model="roleForm.role_name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="角色标识:" prop="identify">
        <el-input v-model="roleForm.identify" placeholder="请输入角色标识" />
      </el-form-item>
      <el-form-item label="角色描述:" prop="description">
        <el-input v-model="roleForm.description" placeholder="请输入角色描述" />
      </el-form-item>
      <el-form-item label="角色权限" prop="authlist">
        <el-tree
          v-if="isShow"
          ref="tree"
          :check-strictly="false"
          :data="menuList"
          node-key="id"
          :default-checked-keys="chechkAuth"
          :props="defaultProps"
          show-checkbox
          accordion
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="isShow=false">取 消</el-button>
      <el-button v-if="roleForm.id" type="primary" @click="updateRole">修改</el-button>
      <el-button v-else type="primary" @click="btnOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { toArray } from '@/utils'
import { getMenuListApi } from '@/api/permission/menu'
import { setRoleAuthApi, getRoleInfoApi, updateRoleApi } from '@/api/permission/role'
export default {
  name: 'RoleList',
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'permission_name'
      },
      isShow: false,
      // 表单数据变量
      roleForm: {
        role_name: '',
        identify: '',
        description: '',
        authlist: []

      },
      chechkAuth: [],
      menuList: [],
      rules: {
        role_name: [
          { required: true, message: '角色名称不能为空', trigger: ['blur', 'change'] }
        ],
        identify: [
          { required: true, message: '角色标识不能为空', trigger: ['blur', 'change'] }
        ],
        description: [
          { required: true, message: '角色描述人不能为空', trigger: ['blur', 'change'] }
        ]
        // /* 选择角色校验 */
        // authlist: [
        //   { required: true, message: '请选择角色', trigger: ['blur'] }
        // ]

      }

    }
  },
  computed: {
    title() {
      return this.roleForm.id ? '修改' : '新增'
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    // 获取树形选择框
    async getMenuList() {
      const res = await getMenuListApi()
      console.log('abv', res)
      this.menuList = res
    },
    // 提交数据
    async btnOk() {
      await this.$refs.form.validate()
      this.roleForm.authlist = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      await setRoleAuthApi(this.roleForm)
      this.$message.success('添加成功')
      this.$emit('updata')
      this.isShow = false
    },
    // 修改数据回显
    async reviewInfo(id) {
      const res = await getRoleInfoApi(id)
      console.log(res)
      this.roleForm = res
      this.chechkAuth = toArray(res.authlist).map(item => item.id)
      this.isShow = true
    },
    // 提交修改数据
    async updateRole() {
      this.roleForm.authlist = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]
      await updateRoleApi(this.roleForm)
      this.$message.success('添加成功')
      this.$emit('updata')
      this.isShow = false
    },
    // 取消重置表单
    closeForm() {
      console.log('关闭')
      // 清除表单中的数据
      this.roleForm = {
        role_name: '',
        identify: '',
        description: '',
        authlist: []
      }
      // 清除验证规则
      setTimeout(() => {
        this.$refs.form.clearValidate()
      }, 300)
      this.chechkAuth = []
    }

  }
}
</script>

<style>

</style>
